package com.cqeec.gams.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqeec.gams.service.AccountService;

@Controller
@RequestMapping("/user/")
public class AccountController {
	@Autowired
	private AccountService accountService;

	@RequestMapping("/change/password.html")
	@ResponseBody
	public Map<String, Object> changePassword(String id, String oldPassword, String newPassword,
			String confirmPassword) {
		Map<String, Object> result = new HashMap<>();
		// 判断新密码和确认密码是否一致
		if (newPassword.equals(confirmPassword)) {
			// 判断原密码是否正确
			boolean b = accountService.checkPassword(id, oldPassword);
			if (b) {
				// 修改密码
				try {
					accountService.updatePassword(id, newPassword);
					result.put("code", 0);
					result.put("message", "修改密码成功！");
				} catch (Exception e) {
					result.put("code", 3);
					result.put("message", "修改密码失败！");
				}
			} else {
				result.put("code", 2);
				result.put("message", "用户原密码错误！");
			}
		} else {
			result.put("code", 1);
			result.put("message", "新密码和确认密码不一致！");
		}
		return result;
	}

}
